﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_CountryZones : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IMasCountryZoneBLL zoneBLL = new MasCountryZoneBLL();
        private IMasCountryBLL countryBLL = new MasCountryBLL();
        private IMasCityBLL cityBLL = new MasCityBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;

                RadGrid1.Visible = false;
                lblHeader.Text = "ข้อมูลภูมิภาค";
            }

        }
        public void LoadDataGridZone()
        {
            IList<MasCountryZoneInfo> Info = zoneBLL.ListMasCountryZones();
            RadGrid1.DataSource = Info;

           // RadGrid1.Columns[3].HeaderText = "รหัสภูมิภาค";
            RadGrid1.Columns[3].HeaderText = "ชื่อภูมิภาค";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MP_CountryZones.ascx";
            RadGrid1.DataBind();
        }
        public void LoadDataGridCountry(Nullable<int> ZoneSeq)
        {
            IList<MasCountryInfo> Info = countryBLL.ListMasCountries("", ZoneSeq.ToString());
            RadGrid1.DataSource = Info;

           // RadGrid1.Columns[3].HeaderText = "รหัสประเทศ";
            RadGrid1.Columns[3].HeaderText = "ชื่อประเทศ";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MAS_Countries.ascx";
            RadGrid1.DataBind();
        }
        public void LoadDataGridCity(string CountrySeq)
        {
            IList<MasCityInfo> Info = cityBLL.ListMasCities("", CountrySeq);
            RadGrid1.DataSource = Info;

            RadGrid1.Columns[3].HeaderText = "ชื่อเมือง";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MAS_Cities.ascx";
            RadGrid1.DataBind();
        }
        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadCountryZoneTreeView("");

            foreach (TreeViewInfo info in treeBLL.LoadCountryZoneTreeView(""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadCountryZoneTreeView(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadCountryZoneTreeView(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }
        public void RefreshTree()
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (selected_node == null)
            {
                selected_node = RadTreeView1.FindNodeByValue("0");
            }
            RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            RadAjaxManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            RadAjaxManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }
        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadCountryZoneTreeView("");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }
        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }
        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadCountryZoneTreeView("");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (RadTreeView1.SelectedNode.Level == 0)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 0)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);

                    zoneBLL.Delete(seq, ssMng.EmpSeqI);                    
                }
                LoadDataGridZone();
            }
            else if (RadTreeView1.SelectedNode.Level == 1)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 1)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                    countryBLL.Delete(seq, ssMng.EmpSeqI);                     
                }
                LoadDataGridCountry(Int32.Parse(Session["ZoneSeq"].ToString()));
            }
            else if (RadTreeView1.SelectedNode.Level == 2)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 2)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                    cityBLL.Delete(seq, ssMng.EmpSeqI); 
                }
                LoadDataGridCity(Session["CountrySeq"].ToString());
            }
            RefreshTree();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            string value = selected_node.Value;
            string name = selected_node.Text.Trim();
            string[] root_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

            if (!string.IsNullOrEmpty(value))
            {
                RadGrid1.Visible = true;

                if (selected_node.Level == 0)
                {
                    LoadDataGridZone();
                    lblHeader.Text = "ข้อมูลภูมิภาค";
                }
                else if (selected_node.Level == 1)
                {
                    LoadDataGridCountry(Int32.Parse(root_value[1]));
                    lblHeader.Text = "ข้อมูลประเทศที่อยู่ภายในภูมิภาค" + name;
                    Session["ZoneSeq"] = root_value[1];
                }
                else if (selected_node.Level == 2)
                {
                    LoadDataGridCity(root_value[1]);
                    lblHeader.Text = "ข้อมูลเมืองที่อยู่ในประเทศ" + name;
                    Session["ZoneSeq"] = root_value[0];
                    Session["CountrySeq"] = root_value[1];
                }  
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void RadTreeView1_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
        {
            //RadTreeNode selected_node = RadTreeView1.SelectedNode;

            //foreach (RadTreeNode node in e.DraggedNodes)
            //{
            //    RadTreeNode destNode = e.DestDragNode;                

            //    string[] root_value = destNode.Value.Split(new string[] { "-" }, StringSplitOptions.None);
            //    string[] node_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

            //    if (root_value[0] == "0")
            //    {
            //        countryInfo.Seq = node.Value.Length > 0 ? Int32.Parse(node_value[1]) : (int?)null;
            //        countryInfo.ZoneSeq = Int32.Parse(root_value[1]);
            //        countryInfo.UpdatedBy = 1;

            //        countryBLL.UpdateZones(countryInfo);
            //    }
            //}

            //BindTreeCommandLine(RadTreeView1);
        }
    }
}
